package com.micro.common.service.orm.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.service.IService;
import com.micro.common.service.orm.entity.DictDataPo;
import com.micro.common.service.orm.mapper.DictDataMapper;
import com.micro.common.service.orm.param.DictQueryParam;
import com.micro.framework.starter.common.data.enums.DeletedEnum;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @description 字典数据表 通用服务实现类
* @author system
* @date   2025-04-07
*/
@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictDataPo> implements IService<DictDataPo>{

    public DictDataPo findByGroupAndCode(String groupCode,String dictCode) {
        return baseMapper.findByGroupAndCode(groupCode,dictCode);
    }

    public Map<String, String> dictMap(String groupCode) {
        List<DictDataPo> poList = baseMapper.dicPageList(null, DictQueryParam.builder().groupCode(groupCode).build());
        return poList.stream().collect(Collectors.toMap(DictDataPo::getDictCode, DictDataPo::getDictName,(a, b) -> a));
    }

    /**
     * 如果是多个字典分组，用groupCode + dictCode作为key
     */
    public Map<String,Map<String, String>> dictMapMulti(List<String> listGroupCode) {
        List<DictDataPo> poList = baseMapper.dicPageList(null, DictQueryParam.builder().groupCodes(listGroupCode).build());
        Map<String,List<DictDataPo>> listMap = poList.stream().collect(Collectors.groupingBy(DictDataPo::getGroupCode));
        Map<String,Map<String, String>> mapMap = new HashMap<>();
        listMap.keySet().forEach(key->{
            mapMap.put(key,listMap.get(key).stream().collect(Collectors.toMap(DictDataPo::getDictCode, DictDataPo::getDictName,(a, b) -> a)));
        });
        return mapMap;
    }
}
